package cn.iocoder.yudao.module.wms.controller.admin.stockserial;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.stockserial.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.stockserial.StockSerialDO;
import cn.iocoder.yudao.module.wms.service.stockserial.StockSerialService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 库存序列号")
@RestController
@RequestMapping("/wms/stock-serial")
@Validated
public class StockSerialController {

    @Resource
    private StockSerialService stockSerialService;

    @PostMapping("/create")
    @Operation(summary = "创建库存序列号")
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:create')")
    public CommonResult<Long> createStockSerial(@Valid @RequestBody StockSerialSaveReqVO createReqVO) {
        return success(stockSerialService.createStockSerial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存序列号")
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:update')")
    public CommonResult<Boolean> updateStockSerial(@Valid @RequestBody StockSerialSaveReqVO updateReqVO) {
        stockSerialService.updateStockSerial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存序列号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:delete')")
    public CommonResult<Boolean> deleteStockSerial(@RequestParam("id") Long id) {
        stockSerialService.deleteStockSerial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存序列号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:query')")
    public CommonResult<StockSerialRespVO> getStockSerial(@RequestParam("id") Long id) {
        StockSerialDO stockSerial = stockSerialService.getStockSerial(id);
        return success(BeanUtils.toBean(stockSerial, StockSerialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存序列号分页")
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:query')")
    public CommonResult<PageResult<StockSerialRespVO>> getStockSerialPage(@Valid StockSerialPageReqVO pageReqVO) {
        PageResult<StockSerialDO> pageResult = stockSerialService.getStockSerialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockSerialRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存序列号 Excel")
    @PreAuthorize("@ss.hasPermission('wms:stock-serial:export')")
    @OperateLog(type = EXPORT)
    public void exportStockSerialExcel(@Valid StockSerialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockSerialDO> list = stockSerialService.getStockSerialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存序列号.xls", "数据", StockSerialRespVO.class,
                        BeanUtils.toBean(list, StockSerialRespVO.class));
    }

}